#include "desktop_icon.h"

#include <QGraphicsDropShadowEffect>
#include <QPainter>

DesktopIcon::DesktopIcon(const bool &is_dir, const QImage &icon, const QString &file_name, QGraphicsItem *parent)
    : QGraphicsItem{parent}, is_dir_(is_dir), icon_(icon),file_name_(file_name)
{
    font_ = QFontDatabase::systemFont(QFontDatabase::GeneralFont);
    font_.setPixelSize(12);
}

DesktopIcon::DesktopIcon(const DesktopIcon &other)
{
    this->is_dir_ = other.is_dir_;
    this->icon_ = other.icon_;
    this->file_name_ = other.file_name_;
    this->width_ = other.width_;
    this->height_ = other.height_;
    this->font_ = other.font_;
}

DesktopIcon &DesktopIcon::operator=(const DesktopIcon &rhs)
{
    this->is_dir_ = rhs.is_dir_;
    this->icon_ = rhs.icon_;
    this->file_name_ = rhs.file_name_;
    this->width_ = rhs.width_;
    this->height_ = rhs.height_;
    this->font_ = rhs.font_;

    return *this;
}

QRectF DesktopIcon::boundingRect() const
{
    return QRectF(QPointF(0,0),QSizeF(width_,height_));
}

void DesktopIcon::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget)
{
    painter->setRenderHints(QPainter::Antialiasing | QPainter::TextAntialiasing | QPainter::SmoothPixmapTransform);
    painter->setPen(Qt::NoPen);
    painter->drawPixmap(QRect(QPoint(15,8),QSize(48,48)),QPixmap::fromImage(icon_));

    QPalette pe;
    pe.setColor(QPalette::WindowText,Qt::white);

    QFontMetrics metrics(font_);
    file_name_ = metrics.elidedText(file_name_,Qt::ElideRight,2*62);

    QGraphicsDropShadowEffect *shadowEffect = new QGraphicsDropShadowEffect();
    // 阴影偏移
    shadowEffect->setOffset(1, 1);
    // 阴影颜色;
    shadowEffect->setColor(QColor(0,0,0));
    // 阴影半径;
    shadowEffect->setBlurRadius(3);

    text_label_.setAlignment(Qt::AlignCenter);
    text_label_.setFont(font_);
    text_label_.setPalette(pe);
    text_label_.setWordWrap(true);
    text_label_.setFixedSize(75,metrics.ascent()*2 + 6);
    // 给窗口设置上当前的阴影效果;
    text_label_.setGraphicsEffect(shadowEffect);
    text_label_.setText(file_name_);

    QPixmap pm = text_label_.grab(text_label_.rect());
    painter->drawPixmap(QRectF(QPointF(3,57),QSizeF(text_label_.size())),pm,text_label_.rect());

}
